﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using AuthenticationService.Client.Controls;

namespace UserManager.Module.CommandBehaviors
{
    public static class LogoutCommand
    {
        private static readonly DependencyProperty LogoutCommandBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "LogoutCommandBehavior",
            typeof(LogoutCommandBehavior),
            typeof(LogoutCommand),
            null);

        public static readonly DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(LogoutCommand),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(LogoutCommand),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(Control control)
        {
            return control.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(Control control, ICommand command)
        {
            control.SetValue(CommandProperty, command);
        }

        public static void SetCommandParameter(Control control, object parameter)
        {
            control.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(Control control)
        {
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            LoginStatus control = dependencyObject as LoginStatus;
            if (control != null)
            {
                LogoutCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            LoginStatus control = dependencyObject as LoginStatus;
            if (control != null)
            {
                LogoutCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static LogoutCommandBehavior GetOrCreateBehavior(LoginStatus control)
        {
            LogoutCommandBehavior behavior =
                control.GetValue(LogoutCommandBehaviorProperty) as LogoutCommandBehavior;
            if (behavior == null)
            {
                behavior = new LogoutCommandBehavior(control);
                control.SetValue(LogoutCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }

    public class LogoutCommandBehavior : CommandBehaviorBase<LoginStatus>
    {
        public LogoutCommandBehavior(LoginStatus control)
            : base(control)
        {
            control.Logout += OnLogout;
        }

        private void OnLogout(object sender, RoutedEventArgs e)
        {
            ExecuteCommand();
        }
    }
}
